//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.baidu.trace;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.Build.VERSION;
import com.baidu.trace.b.d;
import com.baidu.trace.c.e;
import java.lang.ref.WeakReference;
import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.Map.Entry;

public final class bd extends Thread {
    private Context j = null;
    private WeakReference<LBSTraceService> k = null;
    private bd.a l = null;
    private bd.b m = null;
    private Handler n = null;
    private int o = 0;
    private long p = 0L;
    private static Map<Integer, Integer> q = null;
    private static Map<Integer, Queue<com.baidu.trace.ar.a>> r = null;
    protected ar a = null;
    protected static int b;
    protected static Deque<com.baidu.trace.ar.a> c;
    protected static boolean d;
    private boolean s = false;
    private boolean t = false;
    protected boolean e = false;
    protected static int f;
    protected static boolean g;
    protected static boolean h;
    protected static int i;
    private long u = 0L;
    private long v = System.currentTimeMillis();
    private long w = System.currentTimeMillis();

    public bd(LBSTraceService var1) {
        this.k = new WeakReference(var1);
        this.n = ((LBSTraceService)this.k.get()).getTraceHandler();
        this.j = ((LBSTraceService)this.k.get()).getServiceContext();
        this.a = new ar(this.k, this.n);
        this.m = new bd.b(0);
        q = new LinkedHashMap();
        r = new LinkedHashMap();
        c = new LinkedList();
    }

    protected final void a(boolean var1, boolean var2) {
        this.t = var1;
        this.s = var2;
        if(!var2) {
            this.o = 0;
        }

    }

    protected final void a(long var1) {
        this.p = 0L;
    }

    public final void run() {
        Looper.prepare();
        this.l = new bd.a();
        this.l.post(this.m);
        Looper.loop();
    }

    protected final void a() {
        f = 0;
        g = false;
        h = false;
        d = false;
        this.t = false;
        this.s = false;
        this.e = false;
    }

    protected static void b() {
        if(q != null) {
            q.clear();
            q = null;
        }

        if(r != null) {
            r.clear();
            r = null;
        }

        if(c != null) {
            c.clear();
            c = null;
        }

    }

    protected final void c() {
        if(this.l != null && this.m != null) {
            this.l.removeCallbacks(this.m);
            if(VERSION.SDK_INT >= 18) {
                this.l.getLooper().quitSafely();
            } else {
                this.l.getLooper().quit();
            }
        }

        this.m = null;
        this.l = null;
    }

    protected static void d() {
        int var0 = i / ar.b;
        if(ar.b <= z.p * 1000) {
            b = ar.b;
        } else if((ar.b = ar.b / 5000 * 5000) % (z.p * 1000) == 0) {
            b = z.p * 1000;
        } else {
            b = 5000;
        }

        i = ar.b * var0;
    }

    protected static int e() {
        int var0 = 0;
        if(c != null) {
            var0 = 0 + c.size();
        }

        if(aw.a != null) {
            var0 += aw.a.size();
        }

        return var0;
    }

    protected static void a(int var0, Queue<com.baidu.trace.ar.a> var1) {
        r.put(Integer.valueOf(var0), var1);
        q.put(Integer.valueOf(var0), Integer.valueOf(e.a()));
    }

    protected static void a(int var0) {
        r.remove(Integer.valueOf(var0));
        q.remove(Integer.valueOf(var0));
    }

    protected static void a(Handler var0) {
        if(e.d > 0 && z.s == 1 && e.a() - e.d > 60 && var0 != null) {
            var0.obtainMessage(4).sendToTarget();
        }

    }

    protected static void a(boolean var0) {
        if(r != null && 0 != r.size()) {
            Iterator var1 = r.entrySet().iterator();

            while(true) {
                Queue var3;
                Integer var4;
                Integer var7;
                do {
                    do {
                        do {
                            Entry var2;
                            do {
                                if(!var1.hasNext()) {
                                    return;
                                }

                                var2 = (Entry)var1.next();
                            } while(var2 == null);

                            var3 = (Queue)var2.getValue();
                            var7 = (Integer)var2.getKey();
                        } while(var3 == null);
                    } while(var7 == null);

                    var4 = (Integer)q.get(var7);
                } while(var4 == null);

                int var5 = e.a();
                if(var0 && (long)(var5 - var4.intValue()) < aw.b) {
                    var3.clear();
                    return;
                }

                while(var3.size() > 0) {
                    com.baidu.trace.ar.a var8 = (com.baidu.trace.ar.a)var3.poll();
                    aw.a.offerLast(var8);
                }

                try {
                    q.remove(var7);
                    var1.remove();
                } catch (Exception var6) {
                    ;
                }
            }
        }
    }

    static {
        b = ar.b;
        c = null;
        d = false;
        f = 0;
        g = false;
        h = false;
        i = 30000;
    }

    class b implements Runnable {
        private b() {
            this.a = bd.this;
            super();
        }

        public final void run() {
            if(bd.d) {
                if(this.a.t) {
                    bd.a(this.a.n);
                }

                if(null != this.a.a && System.currentTimeMillis() - this.a.u >= (long)ar.b) {
                    this.a.u = System.currentTimeMillis();
                    this.a.a.a();
                }
            }

            int var2;
            if(this.a.s) {
                this.a.o++;
                if((var2 = bd.c.size()) > 0) {
                    if('\uea60' == this.a.o * ar.b || var2 >= '\uea60' / ar.b) {
                        bd.a(this.a, 0);
                        bd.a(false);
                        am.a(false);
                        bd.g = true;
                        bd.h = true;
                    }
                } else {
                    bd.a(this.a, 0);
                }

                if(0L == this.a.p) {
                    this.a.p = (long)e.a();
                }

                if((long)e.a() - this.a.p >= 900L && null != this.a.j) {
                    this.a.p = (long)e.a();
                    Intent var5 = new Intent("com.baidu.trace.action.SOCKET_RECONNECT");
                    this.a.j.sendBroadcast(var5);
                }

                this.a.l.postDelayed(this, (long)bd.b);
            } else {
                int var3;
                if(this.a.t) {
                    bd.a(this.a, 0);
                    bd.a(true);
                    var2 = bd.e();
                    var3 = bd.b;
                    if(bd.g || !bd.d && bd.h) {
                        var3 = 2000;
                    }

                    if(var2 >= bd.i / ar.b || !bd.d && bd.h) {
                        if(!bd.j(this.a) && System.currentTimeMillis() - this.a.w >= (long)(z.p * 1000)) {
                            this.a.w = System.currentTimeMillis();
                            bd.l(this.a);
                        }

                        this.a.v = System.currentTimeMillis();
                    } else if(System.currentTimeMillis() - this.a.v >= (long)bd.i) {
                        this.a.v = System.currentTimeMillis();
                        if(var2 > 0) {
                            if(!bd.j(this.a)) {
                                this.a.w = System.currentTimeMillis();
                                bd.l(this.a);
                            }
                        } else if(System.currentTimeMillis() - this.a.w >= (long)(z.p * 1000)) {
                            this.a.w = System.currentTimeMillis();
                            bd.l(this.a);
                        }
                    } else if(System.currentTimeMillis() - this.a.w >= (long)(z.p * 1000)) {
                        this.a.w = System.currentTimeMillis();
                        bd.l(this.a);
                    }

                    this.a.l.postDelayed(this, (long)var3);
                } else {
                    if(this.a.e) {
                        bd.a(true);
                        Message var1;
                        if((var3 = bd.e()) <= 0 && !bd.h) {
                            byte[] var4 = ax.a(12, e.a());
                            if(var4 != null && d.a(this.a.j) && com.baidu.trace.b.a.d()) {
                                bd.f = e.a();
                                az.a(this.a.n);
                                com.baidu.trace.b.a.a(var4, this.a.n);
                                return;
                            }

                            if(null != this.a.n) {
                                (var1 = this.a.n.obtainMessage(141)).arg1 = 141;
                                var1.sendToTarget();
                            }
                        } else {
                            if(d.a(this.a.j) && com.baidu.trace.b.a.d()) {
                                if(!bd.j(this.a)) {
                                    bd.l(this.a);
                                }

                                this.a.l.postDelayed(this, 2000L);
                                return;
                            }

                            am.a(false);
                            if(null != this.a.n) {
                                var1 = this.a.n.obtainMessage(141);
                                if(var3 <= 0 && !bd.h) {
                                    var1.arg1 = 141;
                                } else {
                                    var1.arg1 = 143;
                                }

                                var1.sendToTarget();
                                return;
                            }
                        }
                    }

                }
            }
        }
    }

    static class a extends Handler {
        a() {
        }

        public final void handleMessage(Message var1) {
            super.handleMessage(var1);
        }
    }
}
